package univ.uchiha.esiag.gon.accesser;

import java.util.Vector;

import univ.uchiha.esiag.gon.operation.Operation;
import univ.uchiha.esiag.gon.publisher.Publisher;
import univ.uchiha.esiag.gon.publisher.Semaphore;


public class DataWriter extends Thread implements DataAccess{
	private Publisher publisher;
	private Vector<Operation> operations;
	private Semaphore semaphore;
	public DataWriter(Publisher publisher) {
		this.publisher = publisher;
		operations = new Vector<Operation>();
		semaphore = new Semaphore();
	}
	public void write(Operation operation){
		operations.add(operation);
		semaphore.signal();
	}
	@Override
	public void run() {
		while(true){
			if(operations.size() == 0){
				semaphore.stop();
			}
			else{
				publisher.put(operations.remove(0));
			}
		}
	}
}
